package com.hl.springcloud.controller;

import com.hl.springcloud.pojo.Dept;
import com.hl.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Liu Huan
 */
@RestController
public class DeptController {
	
	@Autowired
	private DeptService deptService;//调用service层
	
	@Autowired
	private DiscoveryClient client;
	
	@PostMapping("/dept/add")
	public boolean addDept(Dept dept){
		return deptService.addDept(dept);
	}
	
	@GetMapping("/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id){
		return deptService.queryById(id);
	}
	
	@GetMapping("/dept/get/list")
	public List<Dept> queryAll(){
		return deptService.queryAll();
	}
	
	@GetMapping("/dept/get/discover")
	public Object discover(){
		//获取微服务清单
		List<String> services = client.getServices();
		System.out.println("discover:  service:" + services);
		
		//通过 微服务名字 得到具体的一个微服务，打印它的相关信息
		List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
		for (ServiceInstance instance : instances){
			System.out.println(
					instance.getUri() + "\t" + 
					instance.getHost() + "\t" +
					instance.getPort() + "\t" +
					instance.getServiceId()
			);
		}
		return this.client;
	}
	
}
